/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import exatrez.dao.GalleryDAO;
import exatrez.model.Gallery;
import org.hibernate.Query;
import org.hibernate.Session;
import java.util.List;

/**
 *
 * @author zulfadli
 */
public class GalleryManager {
    
    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    @Inject
    private GalleryDAO galleryDAO;
    
    public List<Gallery> findAll() {
        String query = "SELECT a FROM Gallery a WHERE a.galleryId is not null ORDER BY a.vessel ASC, a.yearTaken DESC";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }
    
    public List<Gallery> getRandomGallery() {
        String query = "SELECT a FROM Gallery a WHERE a.galleryId is not null ORDER BY RAND()";
        Query q = sessionProvider.get().createQuery(query);
        q.setMaxResults(24);
        return q.list();
    }
    
    public List<Gallery> getGalleryYear(String vessel, String year) {
        String queryGallery = "SELECT a FROM Gallery a WHERE a.vessel = '" + vessel +"' AND a.yearTaken = '" + year + "'";
        Query q = sessionProvider.get().createQuery(queryGallery);
        return q.list();
    }
    
    public List<Gallery> getYears(String vessel) {
        String queryGallery = "SELECT DISTINCT a FROM Gallery a WHERE a.vessel = '" + vessel +"' ORDER BY a.year_taken DESC";
        Query q = sessionProvider.get().createQuery(queryGallery);
        return q.list();
    }
    
    @Transactional
    public Gallery saveOrUpdate(Gallery gallery) {
        return galleryDAO.saveOrUpdate(gallery);
    }

    @Transactional
    public void deleteGallery(Gallery gallery) {
        galleryDAO.delete(gallery);
    }

    public GalleryDAO getGalleryDAO() {
        return galleryDAO;
    }

    public void setGalleryDAO(GalleryDAO galleryDAO) {
        this.galleryDAO = galleryDAO;
    }
    
    public Gallery getGallery(String id){
        return galleryDAO.findById(Integer.valueOf(id));
    }
    
}
